<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" scroll :max-height="600">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="150px"
      v-loading="formLoading"
    >
      <el-form-item label="供应商企业全称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入供应商企业全称" />
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="complainCode">
        <el-input v-model="formData.complainCode" placeholder="请输入统一社会信用代码" />
      </el-form-item>
      <el-form-item label="注册人姓名" prop="registryName">
        <el-input v-model="formData.registryName" placeholder="请输入注册人姓名" />
      </el-form-item>
      <el-form-item label="注册人邮箱" prop="registryEmail">
        <el-input v-model="formData.registryEmail" placeholder="请输入注册人邮箱" />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="请输入手机号码" />
      </el-form-item>
      <!--      <el-form-item label="密码" prop="password">-->
      <!--        <el-input v-model="formData.password" placeholder="请输入密码" />-->
      <!--      </el-form-item>-->
      <el-form-item label="企业类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择企业类型">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMPLAIN_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="纳税规模" prop="taxScale">
        <el-select v-model="formData.taxScale" placeholder="请选择纳税规模">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.TAX_SCALE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="营业执照含三证合一" prop="businessPic">
        <UploadImg v-model="formData.businessPic" />
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalName">
        <el-input v-model="formData.legalName" placeholder="请输入法人姓名" />
      </el-form-item>
      <el-form-item label="法人身份证" prop="legalNo">
        <el-input v-model="formData.legalNo" placeholder="请输入法人身份证" />
      </el-form-item>
      <el-form-item label="食品经营许可证" prop="foodPic">
        <UploadImg v-model="formData.foodPic" />
      </el-form-item>
      <el-form-item label="税盘照片" prop="taxPlatePic">
        <UploadImg v-model="formData.taxPlatePic" />
      </el-form-item>
      <el-form-item label="注册资本(万)" prop="registeredCapital">
        <el-input v-model="formData.registeredCapital" placeholder="请输入注册资本(万)" />
      </el-form-item>
      <el-form-item label="企业联系人" prop="companyContact">
        <el-input v-model="formData.companyContact" placeholder="请输入企业联系人" />
      </el-form-item>
      <el-form-item label="企业联系人邮箱" prop="companyEmail">
        <el-input v-model="formData.companyEmail" placeholder="请输入企业联系人邮箱" />
      </el-form-item>
      <el-form-item label="企业联系电话" prop="companyTelephone">
        <el-input v-model="formData.companyTelephone" placeholder="请输入企业联系电话" />
      </el-form-item>
      <el-form-item label="企业经营范围" prop="companyScope">
        <el-input v-model="formData.companyScope" placeholder="请输入企业经营范围" />
      </el-form-item>
      <el-form-item label="企业经营地址" prop="companyAddress">
        <el-input v-model="formData.companyAddress" placeholder="请输入企业经营地址" />
      </el-form-item>
      <!-- <el-form-item label="审核状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择审核状态">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMPLAN_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="开户银行" prop="bankName">
        <el-input v-model="formData.bankName" placeholder="请输入开户银行" />
      </el-form-item>
      <el-form-item label="收款账号" prop="bankAccount">
        <el-input v-model="formData.bankAccount" placeholder="请输入收款账号" />
      </el-form-item>
      <el-form-item label="开户地址" prop="bankAddress">
        <el-input v-model="formData.bankAddress" placeholder="请输入开户地址" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { SupplierApi, SupplierVO } from '@/api/erp/purchase/supplier'

/** ERP 供应商 表单 */
defineOptions({ name: 'SupplierForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  complainCode: undefined,
  registryName: undefined,
  registryEmail: undefined,
  mobile: undefined,
  password: undefined,
  type: undefined,
  taxScale: undefined,
  businessPic: undefined,
  legalName: undefined,
  legalNo: undefined,
  foodPic: undefined,
  taxPlatePic: undefined,
  registeredCapital: undefined,
  companyContact: undefined,
  companyEmail: undefined,
  companyTelephone: undefined,
  companyScope: undefined,
  companyAddress: undefined,
  status: undefined,
  bankName: undefined,
  bankAccount: undefined,
  bankAddress: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '供应商企业全称不能为空', trigger: 'blur' }],
  email: [{ required: true, message: '电子邮箱不能为空', trigger: 'blur' }],
  mobile: [{ required: true, message: '手机号码不能为空', trigger: 'blur' }],
  password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
  type: [{ required: true, message: '企业类型不能为空', trigger: 'change' }],
  taxScale: [{ required: true, message: '纳税规模不能为空', trigger: 'change' }],
  businessPic: [{ required: true, message: '营业执照含三证合一不能为空', trigger: 'blur' }],
  legalName: [{ required: true, message: '法人姓名不能为空', trigger: 'blur' }],
  legalNo: [{ required: true, message: '法人身份证不能为空', trigger: 'blur' }],
  companyContact: [{ required: true, message: '企业联系人不能为空', trigger: 'blur' }],
  companyEmail: [{ required: true, message: '企业联系人邮箱不能为空', trigger: 'blur' }],
  companyTelephone: [{ required: true, message: '企业联系电话不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '审核状态不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await SupplierApi.getSupplier(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as SupplierVO
    if (formType.value === 'create') {
      await SupplierApi.createSupplier(data)
      message.success(t('common.createSuccess'))
    } else {
      await SupplierApi.updateSupplier(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    email: undefined,
    mobile: undefined,
    password: undefined,
    type: undefined,
    taxScale: undefined,
    businessPic: undefined,
    legalName: undefined,
    legalNo: undefined,
    foodPic: undefined,
    taxPlatePic: undefined,
    registeredCapital: undefined,
    companyContact: undefined,
    companyEmail: undefined,
    companyTelephone: undefined,
    companyScope: undefined,
    companyAddress: undefined,
    status: undefined,
    bankName: undefined,
    bankAccount: undefined,
    bankAddress: undefined
  }
  formRef.value?.resetFields()
}
</script>
